/*
 * @(#) GameManager.java
 *
 * Copyright 2006 - 2009 by the original authors of Galaxy
 * and all its contributors ("Galaxy.org").
 *
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kyodai;

import javax.microedition.lcdui.game.GameCanvas;

/**
 * {@code GameInputManager} is the manager of this application. The input device
 * can be recorded here. As an interface, there are three useful methods: 
 * {@code processInput} should be called in main loop, {@code doKeyPressed} 
 * should be called in {@code Canvas}'s {@code keyPressed}, {@code doKeyReleased}
 * should be called in {@code Canvas}'s {@code keyReleased}.
 * 
 * @author Cheng Liang
 * @version 1.0
 */
public class GameInputManager {
	
	/** Current key. */
	private int currKey;
	
	private GameScreen gameScreen;
	
	/**
	 * Instantiates a new game manager.
	 * 
	 * @param gameScreen the game screen
	 */
	public GameInputManager(GameScreen gameScreen) {
		this.gameScreen = gameScreen;
	}

	/**
	 * Process input. This should be called in main loop. There are some flags 
	 * in this method, in {@code Canvas} there should be related method that 
	 * invoked here.
	 */
	public void processInput() {
		if ((currKey & Resource.KEY_OK_FLAG) != 0) {
			currKey &= ~Resource.KEY_OK_FLAG;
			 gameScreen.doOk();
		} else if ((currKey & Resource.KEY_UP_FLAG) != 0) {
			currKey &= ~Resource.KEY_UP_FLAG;
			gameScreen.doUp();
		} else if ((currKey & Resource.KEY_DOWN_FLAG) != 0) {
			currKey &= ~Resource.KEY_DOWN_FLAG;
			gameScreen.doDown();
		} else if ((currKey & Resource.KEY_LEFT_FLAG) != 0) {
			currKey &= ~Resource.KEY_LEFT_FLAG;
			gameScreen.doLeft();
		} else if ((currKey & Resource.KEY_RIGHT_FLAG) != 0) {
			currKey &= ~Resource.KEY_RIGHT_FLAG;
			gameScreen.doRight();
		} else if ((currKey & Resource.KEY_HINT_FLAG) != 0) {
			currKey &= ~Resource.KEY_HINT_FLAG;
			gameScreen.doHint();
		} else if ((currKey & Resource.KEY_RESET_FLAG) != 0) {
			currKey &= ~Resource.KEY_RESET_FLAG;
			gameScreen.doReset();
		} else if ((currKey & Resource.KEY_MENU_FLAG) != 0) {
			currKey &= ~Resource.KEY_MENU_FLAG;
			gameScreen.doMenu();
		}
	}

	/**
	 * Do key pressed. It is mainly judge which key has been pressed according
	 * to game key code. If the game key code cannot be identified, its key code
	 * will be used.
	 * 
	 * @param keyCode the key code
	 * @param gameKeyCode the game key code
	 */
	public void doKeyPressed(int keyCode, int gameKeyCode) {
		switch (gameKeyCode) {
		case GameCanvas.FIRE:
			if (((currKey & Resource.KEY_OK) == 0)
					&& ((currKey & Resource.KEY_OK_FLAG) == 0)) {
				currKey = Resource.KEY_OK | Resource.KEY_OK_FLAG;
			}
			break;
		case GameCanvas.UP:
			if (((currKey & Resource.KEY_UP) == 0)
					&& ((currKey & Resource.KEY_UP_FLAG) == 0)) {
				currKey = Resource.KEY_UP | Resource.KEY_UP_FLAG;
			}
			break;
		case GameCanvas.DOWN:
			if (((currKey & Resource.KEY_DOWN) == 0)
					&& ((currKey & Resource.KEY_DOWN_FLAG) == 0)) {
				currKey = Resource.KEY_DOWN | Resource.KEY_DOWN_FLAG;
			}
			break;
		case GameCanvas.LEFT:
			if (((currKey & Resource.KEY_LEFT) == 0)
					&& ((currKey & Resource.KEY_LEFT_FLAG) == 0)) {
				currKey = Resource.KEY_LEFT | Resource.KEY_LEFT_FLAG;
			}
			break;
		case GameCanvas.RIGHT:
			if (((currKey & Resource.KEY_RIGHT) == 0)
					&& ((currKey & Resource.KEY_RIGHT_FLAG) == 0)) {
				currKey = Resource.KEY_RIGHT | Resource.KEY_RIGHT_FLAG;
			}
			break;
		default:
			switch (keyCode) {
			case GameCanvas.KEY_NUM1: // hint
				if (((currKey & Resource.KEY_HINT) == 0)
						&& ((currKey & Resource.KEY_HINT_FLAG) == 0)) {
					currKey = Resource.KEY_HINT | Resource.KEY_HINT_FLAG;
				}
				break;
			case GameCanvas.KEY_NUM3: // reset
				if (((currKey & Resource.KEY_RESET) == 0)
						&& ((currKey & Resource.KEY_RESET_FLAG) == 0)) {
					currKey = Resource.KEY_RESET | Resource.KEY_RESET_FLAG;
				}
				break;
			case GameCanvas.KEY_NUM2: // up
				if (((currKey & Resource.KEY_UP) == 0)
						&& ((currKey & Resource.KEY_UP_FLAG) == 0)) {
					currKey = Resource.KEY_UP | Resource.KEY_UP_FLAG;
				}
				break;
			case GameCanvas.KEY_NUM8: // down
				if (((currKey & Resource.KEY_DOWN) == 0)
						&& ((currKey & Resource.KEY_DOWN_FLAG) == 0)) {
					currKey = Resource.KEY_DOWN | Resource.KEY_DOWN_FLAG;
				}
				break;
			case GameCanvas.KEY_NUM4: // left
				if (((currKey & Resource.KEY_LEFT) == 0)
						&& ((currKey & Resource.KEY_LEFT_FLAG) == 0)) {
					currKey = Resource.KEY_LEFT | Resource.KEY_LEFT_FLAG;
				}
				break;
			case GameCanvas.KEY_NUM6: // right
				if (((currKey & Resource.KEY_RIGHT) == 0)
						&& ((currKey & Resource.KEY_RIGHT_FLAG) == 0)) {
					currKey = Resource.KEY_RIGHT | Resource.KEY_RIGHT_FLAG;
				}
				break;
			case GameCanvas.KEY_NUM5: // fire
				if (((currKey & Resource.KEY_OK) == 0)
						&& ((currKey & Resource.KEY_OK_FLAG) == 0)) {
					currKey = Resource.KEY_OK | Resource.KEY_OK_FLAG;
				}
				break;
			case GameCanvas.KEY_STAR: // pause
				if (((currKey & Resource.KEY_MENU) == 0)
						&& ((currKey & Resource.KEY_MENU_FLAG) == 0)) {
					currKey = Resource.KEY_MENU | Resource.KEY_MENU_FLAG;
				}
				break;
			}
		}
	}

	/**
	 * Do key released. It is mainly judge which key has been released according
	 * to game key code. If the game key code cannot be identified, its key code
	 * will be used.
	 * 
	 * @param keyCode the key code
	 * @param gameKeyCode the game key code
	 */
	public void doKeyReleased(int keyCode, int gameKeyCode) {
		switch (gameKeyCode) {
		case GameCanvas.FIRE:
			currKey &= ~Resource.KEY_OK;
			break;
		case GameCanvas.UP:
			currKey &= ~Resource.KEY_UP;
			break;
		case GameCanvas.DOWN:
			currKey &= ~Resource.KEY_DOWN;
			break;
		case GameCanvas.LEFT:
			currKey &= ~Resource.KEY_LEFT;
			break;
		case GameCanvas.RIGHT:
			currKey &= ~Resource.KEY_RIGHT;
			break;
		default:
			switch (keyCode) {
			case GameCanvas.KEY_NUM1: // hint
				currKey &= ~Resource.KEY_HINT;
				break;
			case GameCanvas.KEY_NUM3: // reset
				currKey &= ~Resource.KEY_RESET;
				break;
			case GameCanvas.KEY_NUM2: // up
				currKey &= ~Resource.KEY_UP;
				break;
			case GameCanvas.KEY_NUM8: // down
				currKey &= ~Resource.KEY_DOWN;
				break;
			case GameCanvas.KEY_NUM4: // left
				currKey &= ~Resource.KEY_LEFT;
				break;
			case GameCanvas.KEY_NUM6: // right
				currKey &= ~Resource.KEY_RIGHT;
				break;
			case GameCanvas.KEY_NUM5: // fire
				currKey &= ~Resource.KEY_OK;
				break;
			case GameCanvas.KEY_STAR: // pause
				currKey &= ~Resource.KEY_MENU;
				break;
			}
		}
	}
	
}
